package com.cipta.lomob;

import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONObject;

import tv.lomob.api.Lomob;
import tv.lomob.api.LomobResponseHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.cipta.lomob.adapter.AdapterPopularUser;
import com.cipta.lomob.base.BaseActivity;
import com.cipta.lomob.model.Users;

public class PopularUserActivity extends BaseActivity{
	private ProgressBar indicator;
	private ListView lvPopularUser;
	private LinkedList<Users> listUser;
	private AdapterPopularUser adapterPopularUser;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_popular_user);
		
		initializeViews();
		initializeLibs();
		initializeProcess();
		initializeActions();
	}
	
	@Override
	public void initializeViews() {
		// TODO Auto-generated method stub
		super.initializeViews();
		indicator = (ProgressBar)findViewById(R.id.pbIndicator);
		lvPopularUser = (ListView)findViewById(R.id.lvPopularUser);
	}
	
	@Override
	public void initializeLibs() {
		// TODO Auto-generated method stub
		super.initializeLibs();
		listUser = new LinkedList<Users>();
	}
	
	@Override
	public void initializeProcess() {
		// TODO Auto-generated method stub
		super.initializeProcess();
		indicator.setVisibility(View.VISIBLE);
		//load pupular users
		Lomob.loadPopularUsers(LomobManager.getInstance().getUser().optString("token"), new LomobResponseHandler() {
			
			@Override
			public void onSuccess(JSONObject result) {
				try{
					indicator.setVisibility(View.GONE);
					JSONArray users = result.optJSONArray("data");
					int total = users.length();
					if(total >0){
						Users user = null;
						//pull data
						for(int i=0; i<total; i++){
							user = new Users();
							user.setId(users.optJSONObject(i).optString("id"));
							user.setName(users.optJSONObject(i).optString("firstname") + " " + users.optJSONObject(i).optString("lastname"));
							user.setAvatar(users.optJSONObject(i).optString("avatar"));
							user.setTotalFollowing(users.optJSONObject(i).optInt("sum_following"));
							user.setTotalFollowers(users.optJSONObject(i).optInt("sum_follower"));
							user.setTotalMomments(users.optJSONObject(i).optInt("sum_moment"));
							int followed = users.optJSONObject(i).optInt("is_following");
							if(followed == 1){
								user.setFollowing(true);
							}else{
								user.setFollowing(false);
							}
							
							listUser.add(user);
						}
						
						//set listener
						adapterPopularUser = new AdapterPopularUser(PopularUserActivity.this,
								listUser, new AdapterPopularUser.PopularUserListener() {
									
									@Override
									public void onFollowClick(int position, final Users user, final View v,
											ViewGroup viewGroup) {
										Button button = (Button) v;
										Log.i("LOMOB", user.getName());
										if(user.isFollowing()){
											//this action for unfollow user											
											button.setBackgroundResource(R.drawable.follow_button);
											Lomob.unFollowUser(LomobManager.getInstance().getUser().optString("token"), user.getId(), new LomobResponseHandler() {
												@Override
												public void onSuccess(JSONObject result) {
													user.setFollowing(false);
													user.setTotalFollowers(result.optInt("sum_follower"));
													user.setTotalFollowing(result.optInt("sum_following"));
													user.setTotalMomments(result.optInt("sum_moment"));
													adapterPopularUser.notifyDataSetChanged();
													Log.i("API", "Unfollow success");
												}
												
												@Override
												public void onError(String message) {
													Log.i("API", "Unfollow fail: " + message);
													adapterPopularUser.notifyDataSetChanged();
												}
											});	
										}else{
											button.setBackgroundResource(R.drawable.following_button);
											//action for follow user
											Lomob.followUser(LomobManager.getInstance().getUser().optString("token"), user.getId(), new LomobResponseHandler() {
												@Override
												public void onSuccess(JSONObject result) {
													user.setFollowing(true);
													user.setTotalFollowers(result.optInt("sum_follower"));
													user.setTotalFollowing(result.optInt("sum_following"));
													user.setTotalMomments(result.optInt("sum_moment"));
													adapterPopularUser.notifyDataSetChanged();
												}
												
												@Override
												public void onError(String message) {
													Log.i("API", "follow fail: " + message);
													adapterPopularUser.notifyDataSetChanged();
												}
											});		
										}
					
									}
								});
						
						lvPopularUser.setAdapter(adapterPopularUser);
					}
				}catch(Exception ex){
					ex.printStackTrace();					
				}
			}
			
			@Override
			public void onError(String message) {
				indicator.setVisibility(View.GONE);
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.connect_friends_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// toggle nav drawer on selecting action bar app icon/title
		
		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_next:
			MainActivity.toMainActivity(PopularUserActivity.this);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public static void toPopularActivity(Activity activity){
		activity.startActivity(new Intent(activity, PopularUserActivity.class));
	}
}
